<?php
class Manage extends MY_Controller
{
	public function __construct()
	{
		log_message('debug', '=====开始调用Announcement->Manage->__construct接口=====');
		parent::__construct();
		$this->loadLanguageFile('application/controllers/Announcement/');
		log_message('debug', '=====调用Announcement->Manage->__construct接口结束=====');
	}
	
	public function showView() {
		$this->load->view('module/Announcement/manage.htm');
	}
	
	public function getListData() {
		log_message('debug', '=====开始调用Announcement->Manage->getListData接口=====');
		$this->verifyPrivilege();
		
		try {
			$page = $this->param->getParam('page', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			$rows = $this->param->getParam('rows', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			
			$sql = "
				select
					<selectColumns>
				from
					`t_data_announcement_task` a
				left join
					`t_data_user` b
				on
					a.`operatorUserId` = b.`userId`
				LEFT JOIN
					`t_data_announcement_task_info` c
				ON
					c.`taskId` = a.`taskId`
				LEFT JOIN
					`t_data_game_area` d
				ON
					d.`gameAreaId` = c.`gameAreaId`
				LEFT JOIN
					`t_data_operator` e
				ON
					d.`operatorId` = e.`operatorId`
				LEFT JOIN
					`t_data_user_operator_privilege` f
				ON
					d.`operatorId` = f.`operatorId`
					AND
					f.`userId` = {$this->currUserId}
				where
					f.`privilegeValue` = 1
			";
			$getTotalSql = str_replace('<selectColumns>', "count(*)", $sql);
			$this->returnData['data']['total'] = $this->gmDatabase->getColumn($getTotalSql);
			$getDataSql = str_replace('<selectColumns>', "a.`taskId`,a.`content`, a.`startDatetime`, a.`endDatetime`, a.`timeInterval`, b.`name` as `operatorUserName`, a.`operatorDatetime`", $sql);
			
			if($page && $rows) {
				$startRow = ($page - 1) * $rows;
				$getDataSql .= " limit {$startRow}, {$rows}";
			}
			
			$this->returnData['data']['rows'] = $this->gmDatabase->getArray($getDataSql);
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用Announcement->Manage->getListData接口结束=====');
	}
	
	public function add() {
		log_message('debug', '=====开始调用Announcement->Manage->add接口=====');
		$this->verifyPrivilege();
		
		try {
			$gameAreaIdString = $this->param->getParam('gameAreaIdString', array('method' => 'string', 'emptyErrorCode' => 201, 'invalidErrorCode' => 1));
			$startDatetime = $this->param->getParam('startDatetime', array('method' => 'string', 'emptyErrorCode' => 501003, 'invalidErrorCode' => 1));
			$endDatetime = $this->param->getParam('endDatetime', array('method' => 'string', 'emptyErrorCode' => 501004, 'invalidErrorCode' => 1));
			$timeInterval = $this->param->getParam('timeInterval', array('method' => 'int', 'min' => 1, 'emptyErrorCode' => 501001, 'invalidErrorCode' => 501002));
			$content = $this->param->getParam('content', array('method' => 'string', 'maxLength' => 256, 'emptyErrorCode' => 1, 'invalidErrorCode' => 1));
			$startDatetimeSecond = strtotime($startDatetime);
			$endDatetimeSecond = strtotime($endDatetime);
			$this->gmDatabase->closeAutoCommit();
			$sql = "
				insert into
					`t_data_announcement_task`
				set
					`taskType` = 1,
					`content` = '{$content}',
					`startDateTime` = '{$startDatetime}',
					`endDateTime` = '{$endDatetime}',
					`timeInterval` = '{$timeInterval}',
					`operatorUserId` = '{$this->currUserId}',
					`operatorDateTime` = now()
			";
			//error_log("notice sql = ".$sql);
			$this->gmDatabase->query($sql);
			$taskId = $this->gmDatabase->getLastInsertId();
			$gameAreaIdArray = explode(',', $gameAreaIdString);
			
			foreach ($gameAreaIdArray as $gameAreaId) {
				$sql = "
					insert into
						`t_data_announcement_task_info`
					set
						`taskId` = '{$taskId}',
						`gameAreaId` = '{$gameAreaId}',
						`status` = 1
				";
				$this->gmDatabase->query($sql);
			}
			
			$this->gmDatabase->commit();
			$this->gmDatabase->openAutoCommit();
			
			//通知游戏服务器
			foreach ($gameAreaIdArray as $gameAreaId) {
			    $this->initGameAreaInfo($gameAreaId);
			    $url = "http://".$this->ipAddress.":".$this->httpPort."/game/services?action=notice&opt=add"
                                                                                                ."&taskId=".$taskId
                                                                                                ."&start=".$startDatetimeSecond
                                                                                                ."&end=".$endDatetimeSecond
                                                                                                ."&interval=".$timeInterval
                                                                                                ."&content=".$content;
			    $res = file_get_contents($url);
			    error_log("url = ".$url);
			    error_log("res = ".$res);
			}
		}
		
		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		catch(MY_Mysql_Exception $e) {
			$this->gmDatabase->rollback();
			$this->setErrorCode(1);
		}
		
		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}
		
		$this->returnJson();
		log_message('debug', '=====调用Announcement->Manage->add接口结束=====');
	}

	public function remove() {
		log_message('debug', '=====开始调用Announcement->Manage->remove接口=====');
		$this->verifyPrivilege();

		try {
			$taskId = $this->param->getParam('taskId', array('method' => 'int', 'emptyErrorCode' => 201, 'invalidErrorCode' => 1));
			$this->gmDatabase->closeAutoCommit();
			
			$sql = "select content from t_data_announcement_task where taskId = '{$taskId}'";
			$noticeInfo = $this->gmDatabase->getRow($sql);
			$content = $noticeInfo['content'];
			
			$sql = "select gameAreaId from t_data_announcement_task_info where taskId = '{$taskId}'";
			$gameAreadIdArr = $this->gmDatabase->getArray($sql);
			
			$sql = "
				delete FROM
				 	`t_data_announcement_task`
				WHERE
					`taskId` = '{$taskId}'
			";
			$this->gmDatabase->query($sql);

			$sql = "
				delete FROM
				 	`t_data_announcement_task_info`
				WHERE
					`taskId` = '{$taskId}'
			";
			$this->gmDatabase->query($sql);

			$this->gmDatabase->commit();
			$this->gmDatabase->openAutoCommit();
			
			//通知服务器
			foreach ($gameAreadIdArr as $data) {
                $gameAreaId = $data['gameAreaId'];
                $this->initGameAreaInfo($gameAreaId);
                $url = "http://".$this->ipAddress.":".$this->httpPort."/game/services?action=notice&opt=remove"
                                                                                        ."&taskId=".$taskId
                                                                                        ."&content=".$content;
                    file_get_contents($url);
			}
		}

		catch(MY_Param_Exception $e) {
			$this->setErrorCode($e->getCode());
		}

		catch(MY_Mysql_Exception $e) {
			$this->gmDatabase->rollback();
			$this->setErrorCode(1);
		}

		catch(Exception $e) {
			$this->setErrorCode($e->getCode());
		}

		$this->returnJson();
		log_message('debug', '=====调用Announcement->Manage->remove接口结束=====');
	}
}